package com.goldmantis.alitrip.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goldmantis.alitrip.common.BaseService;
import com.goldmantis.alitrip.common.SysLogUtil;
import com.goldmantis.alitrip.common.sysenum.EnumLogCatagory;
import com.goldmantis.alitrip.common.sysenum.EnumOperationType;
import com.goldmantis.alitrip.mapper.SysDataPrivilegesMapper;
import com.goldmantis.alitrip.mapper.SysOrgMapper;
import com.goldmantis.alitrip.mapper.SysUserRoleMapper;
import com.goldmantis.alitrip.pojo.*;
import com.goldmantis.alitrip.pojo.SysUserRole;
import com.goldmantis.alitrip.pojo.SysUserRole;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author:Shmily
 * @Description:
 * @Date:Create in 9:46 2018/2/11
 */
@Service
public class SysUserRoleService extends BaseService<SysUserRole> {

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private SysDataPrivilegesMapper sysDataPrivilegesMapper;

    @Autowired
    private SysLogService sysLogService;

    public PageInfo<Map> selectByRoleId(
            Long roleId,
            Long userId,
            Integer offset,
            Integer limit,
            String fullName,
            String account,
            String jobNumber,
            String posName,
            String orgName,
            boolean subdivision) {
        PageHelper.startPage(offset, limit);
        List<Map> objects = sysUserRoleMapper.selectByRoleId(roleId, userId, fullName, account, jobNumber, posName, orgName,subdivision);
        return new PageInfo(objects);
    }

    public PageInfo<Map> selectNoRoleByRoleId(
            Long roleId,
            Long userId,
            Integer offset,
            Integer limit,
            String fullName,
            String account,
            String jobNumber,
            String posName,
            String orgName) {
        PageHelper.startPage(offset, limit);
        List<Map> objects = sysUserRoleMapper.selectNoRoleByRoleId(roleId, userId, fullName, account, jobNumber, posName, orgName);
        return new PageInfo<Map>(objects);
    }

    public void insertBatchNoCategory(SysUserRole sysUserRole) {
        if (!CollectionUtils.isEmpty(sysUserRole.getUserIds())) {
            for (Long userId : sysUserRole.getUserIds()) {
                SysUserRole item = new SysUserRole();
                item.setUserid(userId);
                item.setRoleid(sysUserRole.getRoleid());

                List<SysUserRole> sysUserRoles = this.sysUserRoleMapper.select(item);
                if (CollectionUtils.isEmpty(sysUserRoles)) {
                    super.insert(item);

                    //授权日志
                    SysLog baseSysLog = SysLogUtil.getBaseSysLog();
                    baseSysLog.setCategoryId(EnumLogCatagory.operation.getValue());
                    baseSysLog.setDescription("角色分配用户");
                    baseSysLog.setOperateTypeId(String.valueOf(EnumOperationType.grant.getValue()));
                    baseSysLog.setOperateType(EnumOperationType.grant.getText());
                    baseSysLog.setSourceObjectId(String.valueOf(userId));


                    sysLogService.insert(baseSysLog);
                }
            }
        }
    }

    public void insertBatch(SysUserRole sysUserRole) {

        //记录原先数据
        List<SysUserRole> roleResOrginal = this.sysUserRoleMapper.getWithCategory(sysUserRole.getUserid(), sysUserRole.getCategory(),sysUserRole.getSystemId());

        List<Long> newItems = new ArrayList<>();

        if (!CollectionUtils.isEmpty(sysUserRole.getRoleIds())) {
            for (Long roleId : sysUserRole.getRoleIds()) {

                //防止重复添加
                boolean flag = false;
                for (SysUserRole itemOriginal : roleResOrginal) {
                    if (itemOriginal.getRoleid().longValue() == roleId.longValue()) {
                        flag = true;
                        break;
                    }
                }

                if (!flag) {
                    SysUserRole item = new SysUserRole();
                    item.setRoleid(roleId);
                    item.setUserid(sysUserRole.getUserid());
                    super.insert(item);

                    SysLog baseSysLog = SysLogUtil.getBaseSysLog();
                    baseSysLog.setCategoryId(EnumLogCatagory.operation.getValue());
                    baseSysLog.setDescription("用户分配角色");
                    baseSysLog.setOperateTypeId(String.valueOf(EnumOperationType.grant.getValue()));
                    baseSysLog.setOperateType(EnumOperationType.grant.getText());
                    baseSysLog.setSourceObjectId(String.valueOf(roleId));


                    sysLogService.insert(baseSysLog);

                }
                newItems.add(roleId);

            }
        }


        //删除掉以前有、现在没有的数据
        for (SysUserRole itemOriginal : roleResOrginal) {
            //防止重复添加
            boolean flag = false;
            for (Long itemNew : newItems) {
                if (itemOriginal.getRoleid().longValue() == itemNew.longValue()) {
                    flag = true;
                    break;
                }
            }

            if (!flag) {
                this.sysUserRoleMapper.delete(itemOriginal);
                SysLog baseSysLog = SysLogUtil.getBaseSysLog();
                baseSysLog.setCategoryId(EnumLogCatagory.operation.getValue());
                baseSysLog.setDescription("用户撤销角色");
                baseSysLog.setOperateTypeId(String.valueOf(EnumOperationType.revoke.getValue()));
                baseSysLog.setOperateType(EnumOperationType.revoke.getText());
                baseSysLog.setSourceObjectId(String.valueOf(itemOriginal.getRoleid()));


                sysLogService.insert(baseSysLog);
            }
        }

    }


    public void insertBatchSingleUserMultiRole(SysUserRole sysUserRole) {
        if (!CollectionUtils.isEmpty(sysUserRole.getRoleIds())) {
            for (Long roleId : sysUserRole.getRoleIds()) {
                SysUserRole item = new SysUserRole();
                item.setRoleid(roleId);
                item.setUserid(sysUserRole.getUserid());

                List<SysUserRole> sysUserRoles = this.sysUserRoleMapper.select(item);
                if (CollectionUtils.isEmpty(sysUserRoles)) {
                    super.insert(item);
                }
            }
        }
    }

    public void deleteByUserIdAndRoleIds(Long userId, List<Long> roleIds) {
        if (!CollectionUtils.isEmpty(roleIds)) {
            for (Long roleId : roleIds) {
                SysUserRole item = new SysUserRole();
                item.setUserid(userId);
                item.setRoleid(roleId);
                super.delete(item);
            }
        }
    }

    public List<Map> getRolesByUserId(Long userId) {
        return this.sysUserRoleMapper.getRolesByUserId(userId);
    }

    public List<Map> getRoleCheckInfoBySystemIdAndUserId(Long systemId, Long userId, String category) {
        return this.sysUserRoleMapper.getRoleCheckInfoBySystemIdAndUserId(systemId, userId, category);
    }

    public void permissionCopy(long originUserId, long targetUserId, boolean isCopyRole, boolean isCopyData) {

        //复制菜单权限
        if(isCopyRole){
            this.sysUserRoleMapper.permissionCopy(originUserId,targetUserId);
        }

        //复制数据权限
        if(isCopyData){
            this.sysDataPrivilegesMapper.permissionCopy(originUserId,targetUserId);
        }


    }
}
